// 游戏状态
let gameState = {
    score: 0,
    totalFish: 0,
    isRodCast: false,
    currentWeather: 'sunny',
    fishInBucket: [],
    weatherEffects: null
};

// 鱼类配置
const fishTypes = {
    common: {
        name: '普通鱼',
        probability: 0.6,
        points: 10,
        class: 'fish-common'
    },
    rare: {
        name: '稀有鱼',
        probability: 0.3,
        points: 50,
        class: 'fish-rare'
    },
    legendary: {
        name: '传说鱼',
        probability: 0.1,
        points: 200,
        class: 'fish-legendary'
    }
};

// 天气配置
const weatherTypes = {
    sunny: {
        name: '晴天',
        fishSpawnRate: 1.0,
        background: 'linear-gradient(to bottom, #87CEEB 0%, #98D8E8 50%, #4682B4 100%)'
    },
    rainy: {
        name: '雨天',
        fishSpawnRate: 1.5,
        background: 'linear-gradient(to bottom, #708090 0%, #778899 50%, #2F4F4F 100%)'
    },
    snowy: {
        name: '雪天',
        fishSpawnRate: 0.7,
        background: 'linear-gradient(to bottom, #B0C4DE 0%, #D3D3D3 50%, #4682B4 100%)'
    },
    cloudy: {
        name: '多云',
        fishSpawnRate: 0.9,
        background: 'linear-gradient(to bottom, #696969 0%, #808080 50%, #4682B4 100%)'
    }
};

// 初始化游戏
function initGame() {
    updateUI();
    startFishSpawning();
    setWeather('sunny');
    
    // 添加键盘事件
    document.addEventListener('keydown', handleKeyPress);
}

// 键盘事件处理
function handleKeyPress(event) {
    if (event.code === 'Space') {
        event.preventDefault();
        if (!gameState.isRodCast) {
            castRod();
        } else {
            reelIn();
        }
    }
}

// 甩竿功能
function castRod() {
    if (gameState.isRodCast) return;
    
    const rod = document.getElementById('fishing-rod');
    const line = document.getElementById('fishing-line');
    const castBtn = document.getElementById('cast-button');
    const reelBtn = document.getElementById('reel-button');
    
    // 播放甩竿动画
    rod.classList.add('casting');
    
    setTimeout(() => {
        rod.classList.remove('casting');
        
        // 显示钓鱼线
        line.style.height = '200px';
        
        // 更新UI
        castBtn.style.display = 'none';
        reelBtn.style.display = 'block';
        
        gameState.isRodCast = true;
        
        // 开始钓鱼检测
        startFishing();
    }, 1000);
}

// 收竿功能
function reelIn() {
    if (!gameState.isRodCast) return;
    
    const line = document.getElementById('fishing-line');
    const castBtn = document.getElementById('cast-button');
    const reelBtn = document.getElementById('reel-button');
    
    // 隐藏钓鱼线
    line.style.height = '0';
    
    // 更新UI
    castBtn.style.display = 'block';
    reelBtn.style.display = 'none';
    
    gameState.isRodCast = false;
    
    // 停止钓鱼检测
    stopFishing();
}

// 开始钓鱼检测
let fishingInterval;
function startFishing() {
    fishingInterval = setInterval(() => {
        // 随机钓到鱼的概率
        const catchChance = Math.random();
        const weatherMultiplier = weatherTypes[gameState.currentWeather].fishSpawnRate;
        
        if (catchChance < 0.3 * weatherMultiplier) {
            catchFish();
        }
    }, 2000);
}

// 停止钓鱼检测
function stopFishing() {
    if (fishingInterval) {
        clearInterval(fishingInterval);
        fishingInterval = null;
    }
}

// 钓到鱼
function catchFish() {
    const fishType = getRandomFishType();
    const fish = {
        type: fishType,
        id: Date.now(),
        ...fishTypes[fishType]
    };
    
    // 显示钓鱼提示
    showCatchNotification(fish);
    
    // 添加到鱼桶
    addFishToBucket(fish);
    
    // 更新分数
    gameState.score += fish.points;
    gameState.totalFish++;
    
    // 自动收竿
    setTimeout(() => {
        reelIn();
    }, 1500);
    
    updateUI();
}

// 获取随机鱼类型
function getRandomFishType() {
    const rand = Math.random();
    let cumulative = 0;
    
    for (const [type, config] of Object.entries(fishTypes)) {
        cumulative += config.probability;
        if (rand <= cumulative) {
            return type;
        }
    }
    return 'common';
}

// 显示钓鱼提示
function showCatchNotification(fish) {
    const notification = document.getElementById('catch-notification');
    const text = document.getElementById('catch-text');
    
    text.textContent = `钓到了 ${fish.name}! +${fish.points}分`;
    notification.style.display = 'block';
    
    setTimeout(() => {
        notification.style.display = 'none';
    }, 2000);
}

// 添加鱼到桶中
function addFishToBucket(fish) {
    gameState.fishInBucket.push(fish);
    
    const container = document.getElementById('caught-fish-container');
    const fishElement = document.createElement('div');
    fishElement.className = `bucket-fish ${fish.class}`;
    fishElement.style.left = Math.random() * 80 + 'px';
    fishElement.style.bottom = Math.random() * 20 + 'px';
    
    container.appendChild(fishElement);
    
    // 限制桶中鱼的数量显示
    if (container.children.length > 8) {
        container.removeChild(container.firstChild);
    }
}

// 生成游泳的鱼
function spawnSwimmingFish() {
    const container = document.getElementById('fish-container');
    const fish = document.createElement('div');
    const fishType = getRandomFishType();
    
    fish.className = `fish ${fishTypes[fishType].class}`;
    fish.style.top = Math.random() * 200 + 50 + 'px';
    fish.style.left = '-50px';
    
    // 添加点击事件（装饰性，不影响钓鱼）
    fish.addEventListener('click', () => {
        fish.style.transform = 'scale(1.5) rotate(360deg)';
        setTimeout(() => {
            if (fish.parentNode) {
                fish.parentNode.removeChild(fish);
            }
        }, 500);
    });
    
    container.appendChild(fish);
    
    // 鱼游完后移除
    setTimeout(() => {
        if (fish.parentNode) {
            fish.parentNode.removeChild(fish);
        }
    }, 8000);
}

// 开始生成鱼
function startFishSpawning() {
    setInterval(() => {
        const weatherMultiplier = weatherTypes[gameState.currentWeather].fishSpawnRate;
        if (Math.random() < 0.4 * weatherMultiplier) {
            spawnSwimmingFish();
        }
    }, 3000);
}

// 切换天气
function changeWeather() {
    const weatherKeys = Object.keys(weatherTypes);
    const currentIndex = weatherKeys.indexOf(gameState.currentWeather);
    const nextIndex = (currentIndex + 1) % weatherKeys.length;
    const nextWeather = weatherKeys[nextIndex];
    
    setWeather(nextWeather);
}

// 设置天气
function setWeather(weather) {
    gameState.currentWeather = weather;
    const weatherConfig = weatherTypes[weather];
    
    // 更新背景
    document.getElementById('game-container').style.background = weatherConfig.background;
    
    // 更新天气文本
    document.getElementById('weather-text').textContent = weatherConfig.name;
    
    // 清除之前的天气效果
    clearWeatherEffects();
    
    // 添加新的天气效果
    createWeatherEffects(weather);
}

// 清除天气效果
function clearWeatherEffects() {
    const container = document.getElementById('weather-particles');
    container.innerHTML = '';
    
    if (gameState.weatherEffects) {
        clearInterval(gameState.weatherEffects);
        gameState.weatherEffects = null;
    }
}

// 创建天气效果
function createWeatherEffects(weather) {
    const container = document.getElementById('weather-particles');
    
    switch (weather) {
        case 'rainy':
            gameState.weatherEffects = setInterval(() => {
                for (let i = 0; i < 5; i++) {
                    createRainDrop(container);
                }
            }, 100);
            break;
            
        case 'snowy':
            gameState.weatherEffects = setInterval(() => {
                for (let i = 0; i < 3; i++) {
                    createSnowFlake(container);
                }
            }, 200);
            break;
            
        case 'cloudy':
            createClouds(container);
            break;
    }
}

// 创建雨滴
function createRainDrop(container) {
    const drop = document.createElement('div');
    drop.className = 'rain-drop';
    drop.style.left = Math.random() * 100 + '%';
    drop.style.animationDuration = (Math.random() * 0.5 + 0.5) + 's';
    
    container.appendChild(drop);
    
    setTimeout(() => {
        if (drop.parentNode) {
            drop.parentNode.removeChild(drop);
        }
    }, 2000);
}

// 创建雪花
function createSnowFlake(container) {
    const flake = document.createElement('div');
    flake.className = 'snow-flake';
    flake.style.left = Math.random() * 100 + '%';
    flake.style.animationDuration = (Math.random() * 2 + 2) + 's';
    
    container.appendChild(flake);
    
    setTimeout(() => {
        if (flake.parentNode) {
            flake.parentNode.removeChild(flake);
        }
    }, 5000);
}

// 创建云朵
function createClouds(container) {
    for (let i = 0; i < 3; i++) {
        setTimeout(() => {
            const cloud = document.createElement('div');
            cloud.className = 'cloud';
            cloud.style.width = (Math.random() * 100 + 80) + 'px';
            cloud.style.height = (Math.random() * 40 + 40) + 'px';
            cloud.style.top = Math.random() * 30 + '%';
            cloud.style.left = '-200px';
            cloud.style.animationDuration = (Math.random() * 10 + 15) + 's';
            
            container.appendChild(cloud);
            
            setTimeout(() => {
                if (cloud.parentNode) {
                    cloud.parentNode.removeChild(cloud);
                }
            }, 25000);
        }, i * 5000);
    }
}

// 更新UI
function updateUI() {
    document.getElementById('score').textContent = gameState.score;
    document.getElementById('total-fish').textContent = gameState.totalFish;
    document.getElementById('fish-count').textContent = gameState.fishInBucket.length;
}

// 页面加载完成后初始化游戏
document.addEventListener('DOMContentLoaded', initGame);